<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    public function index(Request $request) {
        // 遍历轮播图
        $data = DB::table('scrolled_img')->where('status', '=', '2')->select('id', 'image')->get();
        // 遍历商品分类
        $cates = DB::table('cates')
                -> where('pid',0)
        		->get();
        $city = $request -> input('city');
        $address = $request -> input('address');
        $businesses = DB::table('businesses')
        	->where(function($query) use ($request){
        		$query -> where('username','like','%'.$request -> input('keyword').'%')
                    -> where('business_address','like','%'.$request -> input['address'].'%')
        			-> where('business_address','like','%'.$request -> input['city'].'%');
        	 })
        	 ->take(8)
        	 ->get();
        return view('home.index.index', ['data' => $data,'cates' => $cates,'businesses' => $businesses,'address' => $address,'city' => $city]);

    }
    // ajax
    public function ajax()
    {

    	$sType = DB::table('cates')
    		-> where('pid',$_GET['id'])
    		->take(8)
    		->get();
    	return response()->json($sType);
    }
    //ajax2加载商家
    public function ajax2()
    {
    	$bus = DB::table('businesses')
    		-> where('cateid',$_GET['cateid'])
    		->take(8)
    		->get();
    	return response() -> json($bus);
    }
    //ajax3,加载更多商家
    public function ajax3()
    {   
        $_GET['cateid'] ? $_GET['cateid'] : 1;
        $_GET['p'] ? $_GET['p'] : 2;
        $more = DB::table('businesses')
            -> where('cateid',$_GET['cateid'])
            -> where('business_address','like','%'.$_GET['address'].'%')
            -> where('business_address','like','%'.$_GET['city'].'%')
            -> skip($_GET['p']*8)
            ->take(8)
            -> get();
        return response() -> json($more);
    }
    //搜索商家
    public function ajax4()
    {
        $business = DB::table('businesses')
            -> where('username','like','%'.$_GET['keyword'].'%')
            -> take(8)
            -> get();
        return response() -> json($business);
    }
}
